﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

class MenuEntry
{
    string text;
    public string Text
    {
        get { return text; }
        set { text = value; }
    }

    bool isSelectable;
    public bool IsSelectable
    {
        get { return isSelectable; }
        set { isSelectable = value; }
    }

    SpriteFont spriteFont;
    public SpriteFont Font
    {
        get { return spriteFont; }
        set { spriteFont = value; }
    }

    SpriteFont descFont;
    public SpriteFont DescriptionFont
    {
        get { return descFont; }
        set { descFont = value; }
    }

    int descSize = Globals.ScreenWidth;
    public int DescriptionSize
    {
        get { return descSize; }
        set { descSize = value; }
    }

    Vector2 position;
    public Vector2 Position
    {
        get { return position; }
        set { position = value; }
    }

    private string description;
    public string Description
    {
        get { return description; }
        set { description = value; }
    }

    Vector2 descPosition;
    public Vector2 DescriptionPosition
    {
        get { return descPosition; }
        set { descPosition = value; }
    }

    private Texture2D texture;
    public Texture2D Texture
    {
        get { return texture; }
        set { texture = value; }
    }

    private Texture2D selectedTexture;
    public Texture2D SelectedTexture
    {
        get { return selectedTexture; }
        set { selectedTexture = value; }
    }

 
    public event EventHandler<EventArgs> Selected;

    protected internal virtual void OnSelectEntry(string sound)
    {
        if (Selected != null)
        {
            Selected(this, EventArgs.Empty);

            AudioManager.PlaySound(sound);
        }
    }

    /// <summary>
    /// Constructs a new menu entry with the specified text.
    /// </summary>
    public MenuEntry(string text)
    {
        this.text = text;
        isSelectable = true;
    }


    public virtual void Update(MenuScreen screen, bool isSelected, GameTime gameTime)
    { }

    public virtual void Draw(MenuScreen screen, bool isSelected, GameTime gameTime)
    {
        // Draw the selected entry in yellow, otherwise white.
        Color color = isSelected ? Color.Red : Color.LightYellow;

        // Draw text, centered on the middle of each line.
        ScreenManager screenManager = screen.ScreenManager;
        SpriteBatch spriteBatch = screenManager.SpriteBatch;

        if (texture != null)
        {
            if (!isSelected)
                spriteBatch.Draw(texture, position, Color.White);
            if (isSelected)
                spriteBatch.Draw(selectedTexture, position, Color.White);
        }  

        if ((spriteFont != null) && !String.IsNullOrEmpty(text))
        {
            if (texture != null)
            {
                Vector2 textSize = spriteFont.MeasureString(text);
                Vector2 textPosition = position + new Vector2(
                    (float)Math.Floor((texture.Width - textSize.X) / 2),
                    (float)Math.Floor((texture.Height - textSize.Y) / 2));
                spriteBatch.DrawString(spriteFont, text, textPosition, color);
            }
            else
            {
                if (isSelectable)
                    spriteBatch.DrawString(spriteFont, text, position, color);
                else
                    spriteBatch.DrawString(spriteFont, text, position, Color.Gray);
            }
        }
        
        if(!String.IsNullOrEmpty(description) && isSelected)
        {
            SpriteFont aux = spriteFont;
            if (descFont != null)
                aux = descFont;


            bool ok = false;

            string[] descriptionWords = description.Split(' ');

            string actualDesc = "";

            int line = 0;

            
            foreach(string str in descriptionWords)
            {
                string auxstr = str;

                if (aux.MeasureString(actualDesc).X > DescriptionSize || str.Contains(System.Environment.NewLine) || str.Contains("\n") || str.Contains("\r"))
                {
                    spriteBatch.DrawString(aux, actualDesc, new Vector2(DescriptionPosition.X, DescriptionPosition.Y + line * 25), Color.LightYellow);
                    line++;

                    actualDesc = "";

                    if (str.Contains(System.Environment.NewLine) || str.Contains("\n") || str.Contains("\r"))
                    {
                        spriteBatch.DrawString(aux, str, new Vector2(DescriptionPosition.X, DescriptionPosition.Y + line * 25), Color.LightYellow);
                        auxstr = "";
                        line++;
                    }

                }
                if (auxstr.Length > 0)
                    actualDesc += " " + auxstr;
            }
            spriteBatch.DrawString(aux, actualDesc, new Vector2(DescriptionPosition.X, DescriptionPosition.Y + line * 25), Color.LightYellow);
        }

        /*if ((spriteFont != null) && !String.IsNullOrEmpty(text))
        {
            spriteBatch.DrawString(spriteFont, text, position, color);
        }*/
    }

}
